package com.qsw.selectview.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.widget.ImageView;

class SVUtil {
    /**
     * dp转px
     *
     * @param context 环境
     * @param dp      dp
     * @return px
     */
    public static int dp2px(Context context, int dp) {
        return (int) (dp * context.getResources().getDisplayMetrics().density + 0.5f);
    }

    /**
     * sp转px
     *
     * @param context 环境
     * @param sp      sp
     * @return px
     */
    public static int sp2px(Context context, int sp) {
        return (int) (sp * context.getResources().getDisplayMetrics().scaledDensity + 0.5f);
    }

    /**
     * 获取图形选择器
     *
     * @param checkedDrawable 选中时的图形
     * @param normalDrawable  未选中时的图形
     * @return 图形选择器
     */
    public static StateListDrawable getDrawableSelector(Drawable checkedDrawable, Drawable normalDrawable) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(new int[]{android.R.attr.state_checked}, checkedDrawable);
        drawable.addState(new int[]{-android.R.attr.state_checked}, normalDrawable);
        return drawable;
    }

    /**
     * 获取颜色选择器
     *
     * @param checkedColor 选中时的颜色
     * @param normalColor  未选中时的颜色
     * @return 颜色选择器
     */
    public static ColorStateList getColorSelector(int checkedColor, int normalColor) {
        int[][] states = new int[][]{
                new int[]{android.R.attr.state_checked},
                new int[]{-android.R.attr.state_checked}
        };
        int[] colors = new int[]{checkedColor, normalColor};
        return new ColorStateList(states, colors);
    }

    /**
     * 获得一个指定的实线边框矩形Drawable
     *
     * @param strokeColor 描边色
     * @param strokeWidth 描边线宽度，单位：pixels
     * @param radius      圆角角度，单位：pixels
     * @return GradientDrawable
     */
    public static Drawable getShapeDrawable(int strokeColor, int strokeWidth, float radius) {
        return getShapeDrawable(ColorStateList.valueOf(Color.TRANSPARENT),
                ColorStateList.valueOf(strokeColor), strokeWidth, 0, 0,
                radius);
    }

    /**
     * 获得一个指定的矩形Drawable
     *
     * @param solidColor  填充色
     * @param strokeColor 描边色
     * @param strokeWidth 描边线宽度，单位：pixels
     * @param dashWidth   虚线宽度，单位：pixels
     * @param dashGap     虚线间距，当dashGap=0dp时，为实线，单位：pixels
     * @param radius      圆角角度，单位：pixels
     * @return GradientDrawable
     */
    public static Drawable getShapeDrawable(int solidColor,
                                            int strokeColor, int strokeWidth,
                                            float dashWidth, float dashGap,
                                            float radius) {
        return getShapeDrawable(ColorStateList.valueOf(solidColor),
                ColorStateList.valueOf(strokeColor), strokeWidth, dashWidth, dashGap,
                radius);
    }

    /**
     * 获得一个指定的矩形Drawable
     *
     * @param solidColors  填充色
     * @param strokeColors 描边色
     * @param strokeWidth  描边线宽度，单位：pixels
     * @param dashWidth    虚线宽度，单位：pixels
     * @param dashGap      虚线间距，当dashGap=0时，为实线，单位：pixels
     * @param radius       圆角角度，单位：pixels
     * @return GradientDrawable
     */
    public static Drawable getShapeDrawable(ColorStateList solidColors,
                                            ColorStateList strokeColors, int strokeWidth,
                                            float dashWidth, float dashGap,
                                            float radius) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        gradientDrawable.setCornerRadius(radius);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            gradientDrawable.setColor(solidColors);
            // 显示一条虚线，线宽为dashWith，虚线之间的空隙宽度为dashGap，当dashGap=0时，为实线
            gradientDrawable.setStroke(strokeWidth, strokeColors, dashWidth, dashGap);
        } else {
            gradientDrawable.setColor(solidColors.getDefaultColor());
            // 显示一条虚线，线宽为dashWith，虚线之间的空隙宽度为dashGap，当dashGap=0时，为实线
            gradientDrawable.setStroke(strokeWidth, strokeColors.getDefaultColor(), dashWidth, dashGap);
        }
        return gradientDrawable;
    }
}
